home *** CD-ROM | disk | FTP | other *** search
/ Mission 3 / Mission 3.zip / Mission 3.iso / texte / qed / src / text.c < prev    next >
C/C++ Source or Header  |  1998-06-13  |  5KB  |  285 lines

  1. #include "global.h"
  2. #include "ausgabe.h"
  3. #include "find.h"
  4. #include "memory.h"
  5. #include "options.h"
  6. #include "rsc.h"
  7. #include "set.h"
  8. #include "text.h"
  9.  
  10. /*
  11.  * Verwaltung der Texte als einfach verkettete Liste
  12. */
  13. static TEXTP    text_list = NULL;
  14.  
  15. static void nullen(TEXTP t_ptr)
  16. {
  17.     t_ptr->cursor_line = FIRST(&t_ptr->text);
  18.     t_ptr->file_date_time = -1L;
  19.     t_ptr->xpos = 0;
  20.     t_ptr->ypos = 0L;
  21.     t_ptr->moved = 0;
  22.     t_ptr->readonly = FALSE;
  23.     t_ptr->blink = t_ptr->block = t_ptr->block_dir = t_ptr->up_down = FALSE;
  24.     t_ptr->cursor = TRUE;
  25.     t_ptr->namenlos = TRUE;
  26.     t_ptr->loc_opt = &local_options[0];
  27.     t_ptr->info_str[0] = EOS;
  28.     t_ptr->filename[0] = EOS;
  29.     t_ptr->filesys = NO_CASE;
  30.     t_ptr->asave = 0;
  31.     t_ptr->max_line = NULL;
  32. }
  33.  
  34. void clear_text(TEXTP t_ptr)
  35. {
  36.     free_textring(&t_ptr->text);
  37.     nullen(t_ptr);
  38. }
  39.  
  40. TEXTP new_text(int link)
  41. {
  42.     TEXTP new, p;
  43.  
  44.     /* Nummer schon vergeben? */
  45.     if (get_text(link) != NULL)
  46.     {
  47.         inote(1, 0, FATALERR, 9);
  48.         return NULL;
  49.     }
  50.     new = (TEXTP)malloc(sizeof(TEXT));
  51.     if (new != NULL)
  52.     {
  53.         new->next = NULL;
  54.  
  55.         /* Erster Text -> Wurzel */
  56.         if (text_list == NULL)
  57.             text_list = new;
  58.         else
  59.         {
  60.             /* Text am Ende der Liste anhängen */
  61.             p = text_list;
  62.             while (p->next != NULL)
  63.                 p = p->next;
  64.             p->next = new;
  65.         }
  66.         new->link = link;
  67.         init_textring(&new->text);
  68.         nullen(new);
  69.         return new;        
  70.     }
  71.     else
  72.         note(1, 0, NOMEMORY);    
  73.     return NULL;
  74. }
  75.  
  76. void destruct_text(TEXTP t_ptr)
  77. {
  78.     TEXTP p;
  79.  
  80.     /* Wurzel? */
  81.     if (t_ptr == text_list)
  82.         text_list = text_list->next;
  83.     else
  84.     {
  85.         /* Vorgänger suchen */
  86.         p = text_list;
  87.         while (p->next != t_ptr)
  88.             p = p->next;
  89.  
  90.         /* und Aushängen */    
  91.         p->next = t_ptr->next;
  92.     }
  93.     kill_textring(&t_ptr->text);
  94.     free(t_ptr);
  95.     t_ptr = NULL;
  96. }
  97.  
  98. TEXTP get_text(int icon)
  99. {
  100.     TEXTP    p;
  101.  
  102.     if (icon == -1) 
  103.         return NULL;
  104.     p = text_list;
  105.     while (p != NULL)
  106.     {
  107.         if (p->link == icon)
  108.             return p;
  109.         p = p->next;
  110.     }
  111.     return NULL;
  112. }
  113.  
  114. void do_all_text(TEXT_DOFUNC func)
  115. {
  116.     TEXTP    p;
  117.     
  118.     p = text_list;
  119.     while (p)
  120.     {
  121.         (*func)(p);
  122.         p = p->next;
  123.     }
  124. }
  125.  
  126. static LOCOPTP get_locopt(char *filename, int fs)
  127. {
  128.     int    i;
  129.     char    m[MUSTER_LEN + 3];
  130.     
  131.     for (i = 2; i < LOCAL_ANZ; i++)
  132.     {
  133.         if (local_options[i].muster[0] != EOS)
  134.         {
  135.             strcpy(m, "*.");
  136.             strcat(m, local_options[i].muster);
  137.             if (filematch(filename, m, fs))
  138.                 return &local_options[i];
  139.         }
  140.     }
  141.     return &local_options[0];
  142. }
  143.  
  144. void set_text_name(TEXTP t_ptr, char *filename, bool namenlos)
  145. {
  146.     if (!namenlos)
  147.     {
  148.         t_ptr->filesys = fs_case_sens(filename);
  149. /*
  150.         if (t_ptr->filesys == NO_CASE)
  151.             str_toupper(filename);
  152. */
  153.     }
  154.     strcpy(t_ptr->filename, filename);    
  155.     t_ptr->namenlos = namenlos;
  156.  
  157.     if (namenlos)
  158.         t_ptr->loc_opt = &local_options[0];
  159.     else if (t_ptr->text.ending == binmode)
  160.         t_ptr->loc_opt = &local_options[1];
  161.     else
  162.         t_ptr->loc_opt = get_locopt(filename, t_ptr->filesys);
  163. }
  164.  
  165. void update_loc_opt(void)
  166. {
  167.     TEXTP    t_ptr;
  168.     
  169.     t_ptr = text_list;
  170.     while (t_ptr != NULL)    
  171.     {
  172.         if (t_ptr->text.ending == binmode)
  173.             t_ptr->loc_opt = &local_options[1];
  174.         else
  175.         {
  176.             if (!t_ptr->namenlos)
  177.                 t_ptr->loc_opt = get_locopt(t_ptr->filename, t_ptr->filesys);
  178.         }
  179.         t_ptr = t_ptr->next;
  180.     }
  181. }
  182.  
  183. /*
  184.  * Leerzeichen/TABs am Zeilenden löschen.
  185. */
  186. bool strip_endings(TEXTP t_ptr)
  187. {
  188.     ZEILEP    lauf;
  189.     int        i;
  190.     char        c;
  191.  
  192.     lauf = FIRST(&t_ptr->text);
  193.     while (!IS_TAIL(lauf))
  194.     {
  195.         for (i=lauf->len; (--i) >= 0; )
  196.         {
  197.             c = TEXT(lauf)[i];
  198.             if (c != ' ' && c != '\t')
  199.                 break;
  200.         }
  201.         i++;
  202.         if (i < lauf->len)                                /* Zeile verkürzen */
  203.         {
  204.             REALLOC(&lauf,i,i-lauf->len);
  205.             t_ptr->moved++;
  206.         }
  207.         NEXT(lauf);
  208.     }
  209.     return (t_ptr->moved != 0);
  210. }
  211.  
  212. /*
  213.  * Längste Zeile suchen
  214. */
  215. int get_longestline(TEXTP t_ptr)
  216. {
  217.     ZEILEP    lauf;
  218.     int        len;
  219.     
  220.     if (t_ptr->max_line != NULL)
  221.     {
  222.         lauf = t_ptr->cursor_line;
  223.         lauf->exp_len = bild_pos(lauf->len, lauf, t_ptr->loc_opt->tab, t_ptr->loc_opt->tabsize) + 1;
  224.     
  225.         if (lauf->exp_len >= t_ptr->max_line->exp_len)    /* länger als die Längste */
  226.         {
  227.             t_ptr->max_line->is_longest = FALSE;    /* alte ist nicht mehr */
  228.             t_ptr->max_line = lauf;
  229.             lauf->is_longest = TRUE;
  230.         }
  231.         else                                    
  232.         {
  233.             if (lauf->is_longest)            /* wurde die Längste kürzer */
  234.                 t_ptr->max_line = NULL;        /*    -> neue längeste suchen */
  235.         }
  236.     }
  237.         
  238.     if (t_ptr->max_line == NULL)            /* neu suche */
  239.     {
  240.         len = 0;
  241.         lauf = FIRST(&t_ptr->text);
  242.         while (!IS_TAIL(lauf))
  243.         {
  244.             if (lauf->exp_len == -1)        /* Länge hat sich geändert */
  245.                 lauf->exp_len = bild_pos(lauf->len, lauf, t_ptr->loc_opt->tab, t_ptr->loc_opt->tabsize) + 1;
  246.             if (lauf->exp_len > len)
  247.             {
  248.                 t_ptr->max_line = lauf;
  249.                 len = lauf->exp_len;
  250.             }
  251.             lauf->is_longest = FALSE;        /* überall löschen */
  252.             NEXT(lauf);
  253.         }
  254.         t_ptr->max_line->is_longest = TRUE;
  255.     }
  256.     return t_ptr->max_line->exp_len;
  257. }
  258.  
  259. int text_still_loaded(char *name)
  260. {
  261.     int    j, i = -1;
  262.     TEXTP    t_ptr;
  263.  
  264.     t_ptr = text_list;
  265.     while (t_ptr != NULL)
  266.     {
  267.  
  268.         if (t_ptr->filesys == FULL_CASE)
  269.             j = strcmp(t_ptr->filename, name);
  270.         else
  271.             j = stricmp(t_ptr->filename, name);
  272.         if (!t_ptr->namenlos && j == 0)
  273.  
  274. /*
  275.         if (!t_ptr->namenlos && filematch(t_ptr->filename, name, t_ptr->filesys))
  276. */
  277.         {
  278.             i = t_ptr->link;
  279.             break;
  280.         }
  281.         t_ptr = t_ptr->next;
  282.     }
  283.     return i;
  284. }
  285.